package com.moon;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;

import java.util.List;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.sys.DemoApp;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class MainActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		boolean res = PreferencesUtils.getBoolean(MainActivity.this, "is_init");
		if(!res)
		{
			startActivity(new Intent(MainActivity.this,GuideActivity.class));
			finish();
			return;
		}
		
		final Handler handle = new Handler(){
			public void handleMessage(Message msg)
			{
				try
				{
					int flag = PreferencesUtils.getInt(MainActivity.this, "notify_flag");
			        PreferencesUtils.putInt(MainActivity.this, "notify_flag", 0);
					((DemoApp)getApplicationContext()).mTab = flag;
					
					List<Activity> list = ((DemoApp) getApplicationContext())
							.getActivityList();
					int count = list.size();
					for (int i = count - 1; i >= 0; i--) {
						Activity act = list.get(i);
						if(!act.getClass().getName().contains("MainActivity"))
						{
							act.finish();
						}
					}
					
					MainTabActivity.mMainTabActivity.finish();
				}catch(Exception e){}
				
				sendMemberRequest();
				
				String username = PreferencesUtils.getString(MainActivity.this, "username");
				if(username != null && username.length() > 0)
				{
					startActivity(new Intent(MainActivity.this,MainTabActivity.class));
				}
				else
				{
					startActivity(new Intent(MainActivity.this,LoginActivity.class));
				}
				
				finish();
			}
		};
		
		new Thread(){
			public void run()
			{
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
				}
				
				handle.sendEmptyMessage(0);
			}
		}.start();
	}
	
	private void sendIdentifyRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject user = JSON.parseObject(response);
				JSONArray array = user.getJSONArray("identitylabel");
				PreferencesUtils.putString(MainActivity.this, "identitylabel", JSON.toJSONString(array));
				((DemoApp)getApplicationContext()).setIdentify();
				
				sendMemberRequest();
			}
		};
		
		RequestParams params = new RequestParams();
		MgqRestClient.get("init", params, loginHandler);
	}
	
	public void sendMemberRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, false, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);
				JSONObject member = ruser.getJSONObject("members");
				if(member != null && member.size() > 0)
				{
					((DemoApp)getApplicationContext()).mMembers = member;
					PreferencesUtils.putString(MainActivity.this, "all_members", JSON.toJSONString(member));
					
					for(String userId : member.keySet())
					{
						JSONObject item = ((DemoApp)getApplicationContext()).mMembers.getJSONObject(userId);
						
						String name = item.getString("alias");
						if(name == null || name.length() == 0) name = item.getString("username");
						
						String path = null;
						try
						{
							path = item.getString("avatar");
						}catch(Exception e){}
						if(path == null || path.length() == 0)
						{
							if(item.getInteger("is_teacher") == 1) path = "http://odv1e0gp0.bkt.clouddn.com/ic_teacher.png";
							else path = "http://odv1e0gp0.bkt.clouddn.com/ic_family.png";
						}
						else path = "http://120.27.129.233/ylbb/" + path;
						RongIM.getInstance().refreshUserInfoCache(new UserInfo(userId,
								name,Uri.parse(path)));
					}
				}
			}
		};
		
		RequestParams params = new RequestParams();
		params.put("id", PreferencesUtils.getString(this, "userId"));
		MgqRestClient.get("get_my_group_member_list", params, loginHandler);
	}
	
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);//must store the new intent unless getIntent() will return the old one
	}
}
